package com.color.admin.log.repository;


import com.color.admin.log.entity.SysLog;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author HaiQiu
 * 日志持久层
 */
@Repository
public interface LogRepository extends JpaRepository<SysLog, String>, JpaSpecificationExecutor<SysLog> {

    /**
     * 系统日志查询近15天日志统计情况
     * @param time 指定时间
     * @return 统计
     */
    @Query(value = "SELECT DATE_FORMAT(create_time,\"%Y-%m-%d\")as day,sum(if(level=\"info\",1,0)) as info,sum(if(level=\"error\",1,0)) as error \n" +
            "FROM `sys_log` where DATE_SUB(CURDATE(), INTERVAL 15 DAY) <= date(create_time)\n" +
            "GROUP BY DATE_FORMAT(create_time,\"%Y-%m-%d\") ORDER BY create_time DESC ",nativeQuery = true)
    List<Map<String, Object>> lately(Date time);
}
